package com.bruce.springai.config;

import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置聊天记忆Advisor
 */
@Configuration
public class ChatMemoryAdvisorConfig {

    @Bean
    public MessageChatMemoryAdvisor chatMemoryAdvisor(ChatMemory chatMemory){
        return MessageChatMemoryAdvisor.builder(chatMemory).build() ;
    }

    @Bean
    public PromptChatMemoryAdvisor promptChatMemoryAdvisor(ChatMemory chatMemory) {
    String templateStr = """
        {instructions}

        以下是历史对话：
        {memory}
        """;

    return PromptChatMemoryAdvisor.builder(chatMemory).systemPromptTemplate(PromptTemplate.builder()
                         .template(templateStr)
                       .build())
            .build();
}

}
